package com.sxproject.controller.admin;


import com.sxproject.pojo.dto.UsersPageQueryDTO;
import com.sxproject.pojo.dto.UsersDto;
import com.sxproject.pojo.entity.Users;
import com.sxproject.pojo.vo.UsersVo;
import com.sxproject.result.PageResult;
import com.sxproject.result.Result;
import com.sxproject.service.IUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-17
 */
@RestController
@RequestMapping("/admin/users")
@Slf4j
@Api(tags = "员工相关接口")
public class UsersController {

    @Autowired
    private IUsersService usersService;

    // 分页查询
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(UsersPageQueryDTO usersPageQueryDTO) {
        log.info("分页查询员工信息:{}", usersPageQueryDTO);
        PageResult pageResult = usersService.pageQuery(usersPageQueryDTO);
        return Result.success(pageResult);
    }

    // 新增员工
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody UsersDto usersDto) {
        log.info("新增员工:{}", usersDto);

        usersService.saveAdd(usersDto);
        return Result.success();
    }

    //根据id批量删除
    @DeleteMapping
    @ApiOperation("批量删除员工")
    public Result delete(@RequestParam String ids) {
        log.info("批量删除员工:{}", ids);
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        usersService.removeByIds(list);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<UsersVo> getById(@PathVariable Integer id) {
        log.info("根据id查询员工信息:{}", id);
        Users byId = usersService.getById(id);
        UsersVo usersVo = new UsersVo();
        BeanUtils.copyProperties(byId, usersVo);
        return Result.success(usersVo);
    }

    // 修改员工
    @PutMapping
    @ApiOperation("修改员工")
    public Result update(@RequestBody UsersDto usersDto) {
        log.info("修改员工:{}", usersDto);
        usersService.update(usersDto);
        return Result.success();
    }
}
